package com.yunzo.coc.common.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.yunzo.coc.common.bean.coc.CotactsNode;
import com.yunzo.coc.model.GroupContactsModel;

public class CotactsNodeUtil {
private List<CotactsNode> returnList = new ArrayList<CotactsNode>();
    
    /**
     * 根据父节点的ID获取所有子节点
     * @param list 分类表
     * @param typeId 传入的父节点ID
     * @return String
     */
    public List<CotactsNode> getChildCotactsNodes(List<GroupContactsModel> list, String typeId) {
        if(list == null && typeId == null) return null;
        for (Iterator<GroupContactsModel> iterator = list.iterator(); iterator.hasNext();) {
        	GroupContactsModel cn = (GroupContactsModel) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (cn.getStr(GroupContactsModel.PARENTIDS).equals(typeId)) {
                recursionFn(list, cn);
            }
            // 二、遍历所有的父节点下的所有子节点
            /*if (CotactsNode.getParentId()==0) {
                recursionFn(list, CotactsNode);
            }*/
        }
        return returnList;
    }
     
    private void recursionFn(List<GroupContactsModel> list, GroupContactsModel cn) {
    	CotactsNode cnode=null;
        List<GroupContactsModel> childList = getChildList(list, cn);// 得到子节点列表
        if (hasChild(list, cn)) {// 判断是否有子节点
        	cnode=new CotactsNode();
        	cnode.setIds(cn.getStr(GroupContactsModel.ID));
        	cnode.setNodename(cn.getStr(GroupContactsModel.CNAMES));
        	cnode.setParentids(cn.getStr(GroupContactsModel.PARENTIDS));
        	cnode.setSnum(getContactsSnum(cn.getStr(GroupContactsModel.MIDS)));
            returnList.add(cnode);
            
            Iterator<GroupContactsModel> it = childList.iterator();
            while (it.hasNext()) {
            	GroupContactsModel n = (GroupContactsModel) it.next();
                recursionFn(list, n);
            }
        } else {
        	cnode=new CotactsNode();
        	cnode.setIds(cn.getStr(GroupContactsModel.ID));
        	cnode.setNodename(cn.getStr(GroupContactsModel.CNAMES));
        	cnode.setParentids(cn.getStr(GroupContactsModel.PARENTIDS));
        	cnode.setSnum(getContactsSnum(cn.getStr(GroupContactsModel.MIDS)));
            returnList.add(cnode);
        }
    }
    /**
     * 得到职位下手机号码数量
     * @param mids 职位所属手机号码
     * @return
     */
    private Integer getContactsSnum(String mids)
    {
    	int snum=0;
    	if(mids!=null)
    	{
    		if(mids.indexOf(",")!=-1)
			{
    			snum=mids.split(",").length;
			}else if(mids.length()==11)
			{
				snum=1;
			}else
				snum=0;	
    	}
    	return snum;
    }
    // 得到子节点列表
    private List<GroupContactsModel> getChildList(List<GroupContactsModel> list, GroupContactsModel cn) {
        List<GroupContactsModel> cotactsNodeList = new ArrayList<GroupContactsModel>();
        Iterator<GroupContactsModel> it = list.iterator();
        while (it.hasNext()) {
        	GroupContactsModel n = (GroupContactsModel) it.next();
            if (n.getStr(GroupContactsModel.PARENTIDS).equals(cn.getStr(GroupContactsModel.ID))) {
            	cotactsNodeList.add(n);
            }
        }
        return cotactsNodeList;
    }
 
    // 判断是否有子节点
    private boolean hasChild(List<GroupContactsModel> list, GroupContactsModel cn) {
        return getChildList(list, cn).size() > 0 ? true : false;
    }

}
